<?php

namespace App\Console\Commands;

use App\Accounts;
use App\RemoteAccounts;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class GetData extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'GetData';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        set_time_limit(0);
        $initValue = 116900;
        
        //获取最高的id值
        $maxId = Redis::get('id_pointer');
        if (!$maxId||$maxId<$initValue){
            Redis::set('id_pointer',$initValue);
            $maxId = $initValue;
        }

        $addAccounts = RemoteAccounts::where('id','>',$maxId)->orderBy('id','asc')->limit(50)->get()->toArray();
        
        var_dump($addAccounts);exit();
        $count = 0;
        foreach ($addAccounts as $account){
            $newAccount = new Accounts();
            $newAccount->appId = $account['appId'];
            $newAccount->ua = $account['ua'];
            $newAccount->name = $account['name'];
            $newAccount->pass = $account['pass'];
            $newAccount->cookies = $account['cookies'];
            $newAccount->accessToken = $account['accessToken'];
            $newAccount->graphData = $account['graphData'];
            $newAccount->ip = $account['ip'];
            $newAccount->country = $account['country'];
            $newAccount->status = $account['status'];
            $newAccount->create_time = $account['create_time'];
            $newAccount->update_time = $account['update_time'];
            $newAccount->bm = $account['bm'];
            $newAccount->page = $account['page'];
            $newAccount->adaccount = $account['adaccount'];
            $newAccount->bill = $account['bill'];
            $newAccount->adaccount_status = $account['adaccount_status'];
            $newAccount->is_prepay_account = $account['is_prepay_account'];
            $newAccount->page_created_time = $account['page_created_time'];
            $newAccount->bm_created_time = $account['bm_created_time'];
            $newAccount->export_time = $account['export_time'];
            $graphData = json_decode($account['graphData'],true);
            //广告账户信息
            if (isset($graphData['adaccounts'])) {
                foreach ($graphData['adaccounts']['data'] as $key => $value) {
                    $adtrust_dsl_usd = $value['currency'] == 'USD' || $value['adtrust_dsl']==-1 ? $value['adtrust_dsl'] : $value['adtrust_dsl'] / $value['account_currency_ratio_to_usd'];
                    //取最大的活或者欠费的账户的信息
                    if (($newAccount->adtrust_dsl_usd < $adtrust_dsl_usd&&in_array($value['account_status'],[1,3]))||($adtrust_dsl_usd==-1&&in_array($value['account_status'],[1,3]))){
                        $newAccount->adaccount++;
                        if ($value['amount_spent'] > 0) {
                            $newAccount->bill++;
                        }
                        $newAccount->currency = $value['currency'];
                        $newAccount->account_status = $value['account_status'];
                        $newAccount->adtrust_dsl = $value['adtrust_dsl'];
                        $newAccount->adtrust_dsl_usd = $adtrust_dsl_usd;
                        $newAccount->is_prepay_account = (int)$value['is_prepay_account'];
                        $newAccount->amount_spent = $value['amount_spent'];
                    }
                }
            }
            //page信息
            if (isset($graphData['accounts'])) {
                foreach ($graphData['accounts']['data'] as $key => $value) {
                    if ($newAccount->page_created_time > $value['page_created_time'] || !$newAccount->page_created_time) {
                        $newAccount->page_created_time = $value['page_created_time'];
                    }
                }
            }
            //bm信息
            if (isset($graphData['businesses'])) {
                foreach ($graphData['businesses']['data'] as $key => $value) {
                    if ($newAccount->bm_created_time > $value['created_time'] || !$newAccount->bm_created_time) {
                        $newAccount->bm_created_time = $value['created_time'];
                    }
                }
            }
            $newAccount->save();
            $count++;
            Redis::set('id_pointer',$account['id']);
        }
        echo "转移{$count}条数据";
    }
}
